# The American Viewer: Political Consequences of Entertainment Media
# Eunji Kim and Shawn Patterson Jr 
# American Political Science Review 
# Replication File [Figure 3]


# libraries 

library(haven)
library(foreign)
library(stargazer)
library(ggplot2)
library(tidyverse)


# load 'figure3' dataset 

twitter <- read_csv('figure3.csv')

twitter$realDonaldTrump <- as.numeric(twitter$realDonaldTrump)
twitter$SenTedCruz <- as.numeric(twitter$SenTedCruz)
twitter$marcorubio <- as.numeric(twitter$marcorubio) 
twitter$JohnKasich <- as.numeric(twitter$JohnKasich)
twitter$JebBush <- as.numeric(twitter$JebBush)

trump <-  sum(twitter$realDonaldTrump)  / 114140
cruz <-   sum(twitter$SenTedCruz)  / 114140
rubio <-  sum(twitter$marcorubio)  / 114140
kasich <-  sum(twitter$JohnKasich)  /114140
bush <-  sum(twitter$JebBush)  / 114140

candidate <- c('trump', 'rubio', 'cruz', 'bush', 'kasich')
proportion <- c(trump, rubio, cruz, bush, kasich)

data <- data.frame(candidate, proportion)

data$candidate <- factor(data$candidate,levels = c('trump', 'rubio', 'cruz', 'bush', 'kasich'))
data$candidate <- c('TRUMP', 'RUBIO', 'CRUZ', 'BUSH', 'KASICH')
data$candidate <- factor(data$candidate, levels = c('TRUMP', 'RUBIO', 'CRUZ', 'BUSH', 'KASICH'))
data$proportion <- c(round(data$proportion, digits=2))
data$percentage <- data$proportion*100

figure3 <- ggplot(data, aes(x=candidate, y=percentage, label=percentage)) + 
  geom_segment( aes(x=candidate, xend=candidate, y=0, yend=percentage)) + 
  geom_point(stat='identity', aes(col=candidate), size=8)  +
  scale_color_manual(name="Proportion", 
                     values = c("#8a0002", "grey33", "grey33", "grey33", "grey33")) + 
  geom_text(color="white", size=4, fontface='bold') +
  labs(title="% @NBCApprentice Twitter Followers" ) + 
  ylim(0, 100) + theme_minimal() + xlab("Republican Primary Candidates") + ylab("Percentage") + 
  coord_flip() +
  theme(legend.position = 'none',
        axis.title.x = element_text(color="black", size=12),
        axis.text.y = element_text(color= "black", size=11),
        axis.text.x = element_text(color= "black", size=11),
        axis.title.y = element_text(color="black", size=12),
        panel.grid.minor = element_blank()) 


jpeg("figure3.jpeg", units="in", width=8, height=3, res=300)

figure3

dev.off()
